package com.gz.purchase.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gz.purchase.R;
import com.gz.purchase.library.pojo.ProductInfo;
import com.gz.purchase.util.ToastUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.List;

/**
 * 首页数据适配器
 * author wanner1991
 */
public class MainProductsAdapter extends BaseAdapter{
    private String TAG = MainProductsAdapter.class.getSimpleName();
    private List<ProductInfo> mDataList;
    private Context mContext;
    private DisplayImageOptions options;

    public MainProductsAdapter(List<ProductInfo> mDataList, Context mContext) {
        this.mDataList = mDataList;
        this.options = buildOptions();
        this.mContext = mContext;
    }

    private DisplayImageOptions buildOptions() {
        return new DisplayImageOptions.Builder()
                .resetViewBeforeLoading()
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .cacheInMemory()
                .cacheOnDisc()
                .showStubImage(R.drawable.drawable_recent_white)
                .showImageOnFail(R.drawable.drawable_recent_white)
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
                .displayer(new RoundedBitmapDisplayer(5))
                .build();
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return mDataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.main_product_item,null);
            holder = new ViewHolder();
            holder.btn_product_selected = (ImageView) view.findViewById(R.id.btn_product_selected);
            holder.iv_product_pic = (ImageView) view.findViewById(R.id.iv_product_pic);
            holder.tv_product_name = (TextView) view.findViewById(R.id.tv_product_name);
            holder.tv_price_price = (TextView) view.findViewById(R.id.tv_price_price);
            holder.tv_product_num = (TextView) view.findViewById(R.id.tv_product_num);
            holder.btn_minus = (Button) view.findViewById(R.id.btn_minus);
            holder.tv_buy_num = (TextView) view.findViewById(R.id.tv_buy_num);
            holder.btn_add = (Button) view.findViewById(R.id.btn_add);

            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }
        final  ProductInfo info = mDataList.get(i);
        // 初始化选中按钮
        if (info.isSelected()){

            holder.btn_product_selected.setBackgroundResource(R.mipmap.ic_shopping_selected);
        }else{
            holder.btn_product_selected.setBackgroundResource(R.mipmap.ic_shopping_unselected);
        }

        holder.btn_product_selected.setTag(info);
        holder.btn_product_selected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProductInfo clickInfo = (ProductInfo) view.getTag();
                if (clickInfo.getProduct_id() == info.getProduct_id()) {
                    if (clickInfo.isSelected()) {
                        clickInfo.setIsSelected(false);
                        view.setBackgroundResource(R.mipmap.ic_shopping_unselected);
                    } else {
                        clickInfo.setIsSelected(true);
                        view.setBackgroundResource(R.mipmap.ic_shopping_selected);
                    }
                }
            }
        });

        // 图片
        ImageLoader.getInstance().displayImage(info.getPic_url(), holder.iv_product_pic, options);
        // 商品名字、价格、库存量、下单数量
        holder.tv_product_name.setText(info.getProduct_name());
        holder.tv_price_price.setText("￥" + info.getProduct_price());
        if(info.getProduct_num() == -1){
            holder.tv_product_num.setText("剩余：");
        }else{
            holder.tv_product_num.setText("剩余："+info.getProduct_num());
        }



        holder.tv_buy_num.setText(info.getSelectNum()+"");
        // 减少数量
        holder.btn_minus.setTag(info);
        holder.btn_minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProductInfo clickInfo = (ProductInfo) view.getTag();
                if (clickInfo.getSelectNum() <= 1){
                    ToastUtils.showToast(mContext,"已达下限");
                    return;
                }else{
                    holder.tv_buy_num.setText((clickInfo.getSelectNum() - 1) + "");
                    clickInfo.setSelectNum(clickInfo.getSelectNum()-1);
                }
            }
        });
        // 增加数量
        holder.btn_add.setTag(info);
        holder.btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProductInfo clickInfo = (ProductInfo) view.getTag();
                if(clickInfo.getProduct_num() != -1){// 如果商品剩余数量是确定的，则增加数量的时候不能超过剩余数量
                    if (clickInfo.getSelectNum() >= clickInfo.getProduct_num()){
                        ToastUtils.showToast(mContext,"已达上限");
                        return;
                    }else{
                        holder.tv_buy_num.setText((clickInfo.getSelectNum() + 1) + "");
                        clickInfo.setSelectNum(clickInfo.getSelectNum()+1);
                    }
                }else{// 如果商品剩余数量是不确定的，则用户添加数量的时候，可以无限制的增加
                    holder.tv_buy_num.setText((clickInfo.getSelectNum() + 1) + "");
                    clickInfo.setSelectNum(clickInfo.getSelectNum()+1);
                }

            }
        });

        return view;
    }

    private class ViewHolder{

        ImageView btn_product_selected;
        ImageView iv_product_pic;
        TextView tv_product_name;
        TextView tv_price_price;
        TextView tv_product_num;
        Button btn_minus;
        TextView tv_buy_num;
        Button btn_add;

    }
}
